home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Programming / AMOSList / AMOSLIST / text0306.txt < prev    next >
Encoding:
Text File  |  1998-04-01  |  3.0 KB  |  104 lines

  1. On 16-Mar-98, Declan_Gorman@modusmedia.com wrote:
  2.  
  3. >     My program has a memory bank which consists of 128 blocks of memory, 
  4. >     each 256k in size.  Each memory block has a name and category and I 
  5. >     want to be able to sort the bank by either.
  6.  
  7. This should do it:
  8.  
  9. <CUT>
  10.  
  11. ' Needs sorting string to be located in S_STR$(0) to S_STR$(NUM-1) 
  12. '   ( ... I don't know how to pass arrays to procs...) 
  13.  
  14. _SORT[128,256*1024,BANK_NUMBER,NUMBER_OF_ITEMS_IN_ARRAY,UNUSED_BANK]
  15.  
  16. Procedure _SORT[NUM,SIZ,BNK,ARR_SIZ,TMP_BNK]
  17.   Shared S_STR$()
  18.   
  19.   Reserve As Work TMP_BNK,SIZ
  20.   Dim CUR(NUM-1),OLD(NUM-1)
  21.   
  22.   'Add number of item to the end of the string 
  23.   For I=0 To NUM-1 : S_STR$(I)=S_STR$(I)+Hex$(I,4) : Next I
  24.   'ensure that unused items in array will be placed first after sorting
  25.   If ARR_SIZ>NUM
  26.     For I=NUM To ARR_SIZ-1 : S_STR$(I)='' : Next I
  27.   End If 
  28.   Sort S_STR$(0)
  29.   'move sorted items FROM last TO first in array 
  30.   For I=0 To NUM-1 : S_STR$(I)=S_STR$(I+ARR_SIZ-NUM) : Next I
  31.   
  32.   'At this point the first NUM indexes in S_STR$() contains sorted 
  33.   'data - followed by the original position in the array.
  34.   
  35.   For I=0 To NUM-1 : CUR(I)=I : OLD(I)=I : Next I
  36.   'CUR(ORIGINAL_POS) contains CURRENT_POS  
  37.   'OLD(CURRENT_POS) contains ORIGINAL_POS
  38.   
  39.   For I=0 To NUM-1
  40.     'OLDI=what index index I had before sorting. 
  41.     OLDI=Val(Right$(S_STR$(I),5))
  42.     'J=Current position of block 
  43.     J=CUR(OLDI)
  44.     Print OLDI,J
  45.     S_STR$(I)=Left$(S_STR$(I),Len(S_STR$(I))-5)
  46.     'swap blocks:
  47.     If I<>J
  48.       Copy Start(BNK)+I*SIZ,Start(BNK)+(I+1)*SIZ To Start(TMP_BNK)
  49.       Copy Start(BNK)+J*SIZ,Start(BNK)+(J+1)*SIZ To Start(BNK)+I*SIZ
  50.       Copy Start(TMP_BNK),Start(TMP_BNK)+SIZ To Start(BNK)+J*SIZ
  51.       Swap CUR(OLD(I)),CUR(OLD(J))
  52.       Swap OLD(I),OLD(J)
  53.     End If 
  54.   Next I
  55.   Erase TMP_BNK
  56. End Proc
  57.  
  58. <CUT>
  59.  
  60. Quick tech explanation (hmmm, how it works, eh):
  61. I use the internal AMOS sort cmd. But before sorting I add the hex number to
  62. the end of the strings. So if the strings are:
  63.  
  64. Hi
  65. There
  66. !!!
  67.  
  68. then I add like this:
  69.  
  70. Hi$0000
  71. There$0001
  72. !!!$0002
  73.  
  74. After sorting (using "Sort") it is:
  75.  
  76. !!!$0002
  77. Hi$0000
  78. There$0001
  79.  
  80. - and I can use the numbers to move around in the bank.
  81.  
  82. I reserve a temp bank on the size of one block (that is 256k)
  83.  
  84. As I do not have 32MB of ram I have not been able to test it with your amount
  85. of memory. It moves the memory three times. That is: It will move 96MB of
  86. data! So it'll probably use a small amount of time.
  87.  
  88. Just write a letter to the list (send a copy to me personally, as I am only
  89. very seldom at home and therefore incidentally skips some letters in the
  90. mailinglists) if you have questions.
  91.  
  92. -- 
  93.  
  94.          /¯\ __    __ /¯¯¯¯¯\           _         Rune Zedeler
  95. ________/ /// \\__/ \\\  ---/           \¯-_      Peter Rørdams Vej 19
  96. \      / //¯|  \\/  ||¯\ \\¯¯¯¯¯¯¯¯¯¯¯¯¯¯   ¯-_   2800 Lyngby
  97.  )    / //  | \ ` / ||  \ \\ Lemmus of Efreet  -  Denmark
  98. /    / ¯¯¯¯¯\\|\-'/ /¯¯¯¯¯ \\____________   _-¯
  99. ¯¯¯¯¯\------'/||¯¯| \------'/           /_-¯      rzedeler@post10.tele.dk
  100.       ¯¯¯¯¯¯\-'/  \-'/¯¯¯¯¯¯            ¯         Tel: +45-45871730
  101.              ¯¯    ¯¯
  102.  
  103.  
  104.